package com.qf.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by 54110 on 2021/5/12.
 */
public class AlipayUtils {


    public String pay(Map map){
        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , "2016101800712391", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQChgaarYoUZKimIgQu8zYWcq5Wc+X7QB2qShINxzQAcsN3KvRvbTcMjvqm80Pd553TMvlK+QKbwrtmREahLKmwLj3qDMxTmAMHhbcWfSp7zT4AdhBhoXtpaecjtcV4SRWPqOhv0rEy/p0nZxb/gOmKedSHxsMgkTc+9pNTKr5ri8Q4jSYkzR2VY/UakpxovLGf4csINqpQZOyE1xwQn2kJ4XNcb/10kbUy8f7LMQ6V3LfVHwVui40H7Tc2QdNCFVnIMbxvyJScps0z+L01erKavHHrX5oQAsCrrwcA/8EZaZ3MuBaippkMHLyCN8Kp9yv+PwPaRtjaYPcUfeKBKP1ebAgMBAAECggEAXptJ/J8LhIA0MCgXrhNeLzY7v+ZOqKGPhiBltyX06NmaMoawaLiWlLu9RkPKk/hHbSAX7CaHI/norJgBU3aYL/VuOKiVl13eNKVO3E6j6bcqPyAABDN8CP/J+X0tM9q9+pjrg1ni42mlUxIJodnRpyzKI4AA3XNPO7HwjIJtYn4PbQ8cRBCILkiVRYQXDPuVMxeUBkl3nQ3YNjBKl1ogUxOaV4+DLhmZ1KkMnZJaQDePnuPJ3J/dyX1XAjCriRxAcuj3QB60eZ1fhHeXytXrgIWactEyyFkGRpFJwG+9EURxcIKkx5rhwCJPnH3fAoFpyMVY780UlL3fwFbA/xym4QKBgQDb2XZaoheaB3AqbjdYsawjbzOM9h8SboMivxJaMiudpf8FTp4j0sy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8tl2hkI8N5sWU4LZAdT8odCSdpWwoMRvAapN+Doa4ceRgBmSc9D6l2qfz8Y0DIjd9wrs2fAc0XUUxb+JG1p5wGnzDagaNxFO8YkfeKlnaLtQ2ELQ2cy25db6sdNihIvXs6J3RzaU9kB4lHZwIDAQAB", "RSA2");  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        //同步回调 --》页面
        alipayRequest.setReturnUrl( "http://331t2j2729.zicp.vip/#/order" );
        //异步回调---》状态 信息
        alipayRequest.setNotifyUrl( "http://8ab2xn.natappfree.cc/ali/notifyPay" ); //在公共参数中设置回跳和通知地址
//        alipayRequest.setBizContent( "{"  +
//                "    \"out_trade_no\":\"238492379723965924659\","  +
//                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                "    \"total_amount\":88.88,"  +
//                "    \"subject\":\"Iphone6 16G\","  +
//                "    \"body\":\"Iphone6 16G\","  +
//                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
//                "    \"extend_params\":{"  +
//                "    \"sys_service_provider_id\":\"2088511833207846\""  +
//                "    }" +
//                "  }" ); //填充业务参数
        Map payMap  =new HashMap<>();
        payMap.put("out_trade_no",map.get("orderid"));
        payMap.put("product_code","FAST_INSTANT_TRADE_PAY");
        payMap.put("total_amount",map.get("totalPrice"));
        payMap.put("subject","千锋支付");
        payMap.put("body","千锋");
        //将map转为 json格式
        String string = JSONObject.toJSONString(payMap);
        alipayRequest.setBizContent(string);
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }

    // 将request中的参数转换成Map
    public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();

        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;

            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }

        return retMap;
    }
}
